import { Body, Controller, Get, Param, Post, Put, Query, Req, UsePipes } from '@nestjs/common';
import { isNumber } from 'class-validator';
import dtoValidationPipe from 'src/pipe/Dto.pipe';
import { errorReturn, successReturn } from 'src/utils';
import { ChargeStrategyService } from './charge-strategy.service';
import ListDto, { AddDto, EditDto } from './dto';

@Controller('chargeStrategy')
export class ChargeStrategyController {
    constructor(
        private chargeStrategyService:ChargeStrategyService
    ){}

    @Get('list')
    @UsePipes(dtoValidationPipe({skipMissingProperties : true}))
    list(@Query() dto:ListDto) {
        return this.chargeStrategyService.list(dto, 0)
    }

    @Get('list/:name')
    strategyNameList(@Param('name') name:string) {
        return this.chargeStrategyService.queryStrategyName(name)
    }

    @Post('add')
    @UsePipes(dtoValidationPipe())
    async addMerchant(@Body() dto:AddDto) {
       try {
            const rs = await this.chargeStrategyService.add(dto, 0)
            return rs ? successReturn('保存成功') : errorReturn('保存失败')
       } catch (error) {
           return errorReturn(error.message)
       }
    }

    @Get('edit/:strategyId')
    async getEditData(@Param('strategyId') strategyId:number) {
        strategyId = Number(strategyId)
        if(!isNumber(strategyId) || strategyId <= 0) return errorReturn()
        const data = await this.chargeStrategyService.getEditData(strategyId, 0)
        return successReturn(data)
    }

    @Put('edit/:strategyId')
    @UsePipes(dtoValidationPipe({skipMissingProperties : true}))
    async edit(@Param('strategyId') strategyId:number, @Body() dto:EditDto, @Req() { merchantId }) {
        strategyId = Number(strategyId)

        if(!isNumber(strategyId) || strategyId <= 0) return errorReturn()
        const data = await this.chargeStrategyService.edit(strategyId, 0, dto)
        return successReturn(data)
    }
}
